#include<iostream>

using namespace std;

//显示菜单
void ShowMenu()
{
    cout<<"1、添加联系人"<<endl;
    cout<<"2、显示联系人"<<endl;
    cout<<"3、删除联系人"<<endl;
    cout<<"4、查找联系人"<<endl;
    cout<<"5、修改联系人"<<endl;
    cout<<"6、清空联系人"<<endl;
    cout<<"0、退出通讯录管理系统"<<endl;
}

const int MAX=100;

//联系人结构体
struct Person
{
    string Name;//姓名

    int Gender;//性别

    int Age;//年龄

    string Phone;//电话

    string Address;//地址

};

struct AddressBooks
{
    Person arr[MAX];

    int size;
};

//添加联系人
void addPerson(AddressBooks &adb)
{
    //判断是否满了
    if(adb.size==MAX)
    {
        cout<<"通讯录已满，无法添加"<<endl;
        return ;
    }

    //添加

    //姓名
    string name;
    cout<<"请输入姓名："<<endl;
    cin>>name;
    adb.arr[adb.size].Name=name;

    //性别
    int gender=0;
    while(true)
    {
        cout<<"请输入性别："<<endl;
        cout<<"1 -> 男"<<endl;
        cout<<"2 -> 女"<<endl;
        cin>>gender;
        if(gender==1||gender==2)
        {
            adb.arr[adb.size].Gender=gender;
            break;
        }
        cout<<"输入有误！重新输入！"<<endl;
    }

    //年龄
    int age=0;
    while(true)
    {
        cout<<"请输入年龄："<<endl;
        cin>>age;
        if(age>0)
        {
            adb.arr[adb.size].Age=age;
            break;
        }
        cout<<"输入有误！重新输入！"<<endl;
    }

    //电话
    string phone;
    cout<<"请输入电话："<<endl;
    cin>>phone;
    adb.arr[adb.size].Phone=phone;

    //住址
    string address;
    cout<<"请输入家庭住址："<<endl;
    cin>>address;
    adb.arr[adb.size].Address=address;

    //增加
    adb.size++;
    cout<<"添加成功！"<<endl;

    //清屏
    system("pause");
    system("cls");
}

//显示联系人
void showPerson(AddressBooks &adb)
{
    //联系人为空
    if(adb.size==0)
    {
        cout<<"当前通讯录为空"<<endl;
    }
    else
    {
        for(int i=0;i<adb.size;i++)
        {
            Person cur=adb.arr[i];

            cout<<"姓名："<<cur.Name<<"   ";
            cout<<"性别："<<(cur.Gender==1?"男":"女")<<"   ";
            cout<<"年龄："<<cur.Age<<"   ";
            cout<<"电话："<<cur.Phone<<"   ";
            cout<<"地址："<<cur.Address<<endl;
        }
    }

    //清屏
    system("pause");
    system("cls");
}

//检测某个联系人是否存在
int isExist(AddressBooks &adb,string name)
{
    for(int i=0;i<adb.size;i++)
    {
        if(adb.arr[i].Name==name)
        {
            return i;
        }
    }
    return -1;
}

//删除联系人
void deletePerson(AddressBooks &adb)
{
    string name;
    cout<<"请输入要删除的联系人："<<endl;
    cin>>name;

    int pos=isExist(adb,name);
    if(pos==-1)
    {
        cout<<"查无此人！"<<endl;
    }
    else
    {
        //数据迁移
        for(int i=pos;i<adb.size;i++)
        {
            adb.arr[i]=adb.arr[i+1];
        }
        adb.size--;

        cout<<"删除成功"<<endl;
    }

    //清屏
    system("pause");
    system("cls");
}

//查找联系人
void findPerson(AddressBooks &adb)
{
    string name;
    cout<<"请输入要查找的联系人姓名："<<endl;
    cin>>name;

    int pos=isExist(adb,name);
    if(pos==-1)
    {
        cout<<"查无此人！"<<endl;
    }
    else
    {
        Person cur=adb.arr[pos];

        cout<<"姓名："<<cur.Name<<"   ";
        cout<<"性别："<<(cur.Gender==1?"男":"女")<<"   ";
        cout<<"年龄："<<cur.Age<<"   ";
        cout<<"电话："<<cur.Phone<<"   ";
        cout<<"地址："<<cur.Address<<endl;
    }
    
    //清屏
    system("pause");
    system("cls");
}

//修改联系人
void changePerson(AddressBooks &adb)
{
    string name;
    cout<<"请输入要修改的联系人姓名："<<endl;
    cin>>name;

    int pos=isExist(adb,name);
    if(pos==-1)
    {
        cout<<"查无此人！"<<endl;
    }
    else
    {
        //姓名
        string name;
        cout<<"请输入姓名："<<endl;
        cin>>name;
        adb.arr[pos].Name=name;

        //性别
        int gender=0;
        while(true)
        {
            cout<<"请输入性别："<<endl;
            cout<<"1 -> 男"<<endl;
            cout<<"2 -> 女"<<endl;
            cin>>gender;
            if(gender==1||gender==2)
            {
                adb.arr[pos].Gender=gender;
                break;
            }
            cout<<"输入有误！重新输入！"<<endl;
        }

        //年龄
        int age=0;
        while(true)
        {
            cout<<"请输入年龄："<<endl;
            cin>>age;
            if(age>0)
            {
                adb.arr[pos].Age=age;
                break;
            }
            cout<<"输入有误！重新输入！"<<endl;
        }

        //电话
        string phone;
        cout<<"请输入电话："<<endl;
        cin>>phone;
        adb.arr[pos].Phone=phone;

        //住址
        string address;
        cout<<"请输入家庭住址："<<endl;
        cin>>address;
        adb.arr[pos].Address=address;
    }
    
    //清屏
    system("pause");
    system("cls");
}

//清空联系人
void clearPerson(AddressBooks &adb)
{
    int clear=0;
    
    while(true)
    {
        cout<<"是否确定清空？"<<endl;
        cout<<"1 -> 是"<<endl;
        cout<<"0 -> 否"<<endl;
        cin>>clear;

        if(clear==1)
        {
            adb.size=0;
            cout<<"通讯录已清空"<<endl;
            break;
        }
        else if(clear==0)
        {
            cout<<"清空操作已取消"<<endl;
            break;
        }
        cout<<"输入有误！重新输入！"<<endl;
    }

    //清屏
    system("pause");
    system("cls");
}

int main()
{

    //创建
    AddressBooks adb;
    adb.size=0;

    //选项
    int select=0;

    while(true)
    {
        //菜单
        ShowMenu();

        cin>>select;
        if(select<0||select>6)
        {
            cout<<"输入有误！请重新输入"<<endl;

            //清屏
            system("pause");
            system("cls");
        }
        else
        {
            switch(select)
            {
                case 1://添加联系人
                {
                    addPerson(adb);
                    break;
                }
                case 2://显示联系人
                {
                    showPerson(adb);
                    break;
                }
                case 3://删除联系人
                {
                    deletePerson(adb);
                    break;
                }
                case 4://查找联系人
                {
                    findPerson(adb);
                    break;
                }
                case 5://修改联系人
                {
                    changePerson(adb);
                    break;
                }
                case 6://清空联系人
                {
                    clearPerson(adb);
                    break;
                }
                case 0://退出通讯录管理系统
                {
                    cout<<"欢迎下次使用！"<<endl;
                    //保持窗口打开
                    system("pause");
                    return 0;
                }
                default:
                {
                    break;
                }
            }
        }
    }

    return 0;
}